// router.js
import { createRouter, __dynamicImportComponent__ } from "@/uni-simple-router";
import { login } from "@/util/login";

import { useCookies } from "vue3-cookies";
const { cookies } = useCookies();
const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  routes: [
    {
      path: `/`,
      name: "index",
      component: __dynamicImportComponent__(`@/pages/index/index.vue`, {
        pageType: `top`,
        style: {
          navigationStyle: "custom",
        },
      }),
      meta: { title: "任务" },
    },
    {
      path: `/login`,
      name: "login",
      meta: { title: "登录" },
      component: __dynamicImportComponent__(`@/pages/login/login.vue`, {
        pageType: `top`,
        style: {
          navigationStyle: "custom",
        },
      }),
    },
    {
      path: `/info`,
      name: "info",
      component: __dynamicImportComponent__(`@/pages/info/info.vue`, {
        pageType: `top`,
      }),
    },
    {
      path: `/preview`,
      name: "preview",
      meta: { title: "预览" },
      component: __dynamicImportComponent__(`@/pages/preview/preview.vue`, {
        pageType: `top`,
      }),
    },
	{
	  path: `/setPhrase`,
	  name: "setPhrase",
	  meta: { title: "快捷输入管理" },
	  component: __dynamicImportComponent__(`@/pages/setPhrase/setPhrase.vue`, {
	    pageType: `top`,
	  }),
	},
  ],
});

//前置路由
router.beforeEach(async (to, from) => {
  if (to.path === "/") {
    if (to.query.code) {
      let flag = await login({ code: to.query.code,state: to.query.state });
      if (!flag) {
        return { path: "/login" };
      }
    } else {
      if (!cookies.isKey("__token")) {
        return { path: "/login" };
      }
    }
  }
});

export default router;
